<template>
  <div id="app">
    <el-menu
      :default-active="activeIndex"
      mode="horizontal"
      background-color="#545c64"
      text-color="#fff"
      :router="asRouter"
      active-text-color="#ffd04b">
      <el-menu-item index="/cardAdd">新增卡片</el-menu-item>
      <el-menu-item index="/cardList">卡片列表</el-menu-item>
      <el-menu-item index="/cardLike">喜欢</el-menu-item>
    </el-menu>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      activeIndex: '1',
      asRouter: true
    }
  },
  // 页面挂载的时候 添加一个浏览器刷新的监听时间 刷新时调用saveState
  mounted() {
    window.addEventListener('unload', this.saveState);
  },
  methods: {
    saveState() {
      // 把$store中的保存保存到浏览器的sessionStorage里
      sessionStorage.setItem('state', JSON.stringify(this.$store.state));
    }
  },
  beforeCreate () {
    document.querySelector('body').setAttribute('style', 'background:#a0522d')
  },
  beforeDestroy () {
    document.querySelector('body').removeAttribute('style')
  }
}
</script>

<style>
</style>
